import Vue from 'vue';
import Router from 'vue-router';
import Home from './components/Home.vue';
import About from './components/About.vue';
import Login from './components/Login.vue';
import CandidateList from './components/Candidate/CandidateList.vue';
import CreateCandidate from './components/Candidate/CreateCandidate.vue';
import JobList from './components/Job/JobList.vue';
import CreateJob from './components/Job/CreateJob.vue';
import DigitalInterviewerManagement from './components/Interviewer/DigitalInterviewerManagement.vue';
import QuestionLibList from './components/QuestionLib/QuestionLibList.vue';
import { setLocalStorageWithExpire, getLocalStorageWithExpire,removeItem } from '@/localStorageUtil.js';



Vue.use(Router);

const routes = [
    {
        path: '/',
        name: 'home',
        component: Home,
        meta: { requiresAuth: true } // 需要登录才能访问
    },
    {
        path: '/about',
        name: 'about',
        component: About,
        meta: { requiresAuth: true }
    },
    {
        path: '/candidateList',
        name: 'CandidateList',
        component: CandidateList,
        meta: { requiresAuth: true }
    },
    {
        path: '/createJob',
        name: 'CreateJob',
        component: CreateJob,
        meta: { requiresAuth: true }
    },
    {
        path: '/digitalInterviewerManagement',
        name: 'DigitalInterviewerManagement',
        component: DigitalInterviewerManagement,
        meta: { requiresAuth: true }
    },
    {
        path: '/createCandidate',
        name: 'CreateCandidate',
        component: CreateCandidate,
        meta: { requiresAuth: true }
    },


    {
        path: '/questionLibList',
        name: 'QuestionLibList',
        component: QuestionLibList,
        meta: { requiresAuth: true }
    },
    {
        path: '/jobList',
        name: 'JobList',
        component: JobList,
        meta: { requiresAuth: true }
    },
    {
        path: '/login',
        name: 'login',
        component: Login,
        meta: { requiresAuth: false } // 不需要登录即可访问
    }
];

const router = new Router({
    routes
});

// 全局路由守卫，用于判断登录状态并决定是否允许访问页面
router.beforeEach((to, from, next) => {
    const isLoggedIn = getLocalStorageWithExpire('isLoggedIn'); // 模拟从本地存储获取登录状态，实际可替换为与后端验证的逻辑
    if (to.meta.requiresAuth &&!isLoggedIn) {
        next('/login'); // 如果需要登录但未登录，跳转到登录页面
    } else {
        next();
    }
});

export default router;